<?php
namespace Admin;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Admin\Model\UserTable;
use Admin\Model\User;
use Admin\Model\AdminProductTable;
use Admin\Model\AdminProduct;
use Admin\Model\Bill;
use Admin\Model\BillTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;


class Module
{
    public $tmp;
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $eventManager->attach(MvcEvent::EVENT_DISPATCH, array($this,'onDispatch'));
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }

    // Add this method:
    public function getServiceConfig()
    {
        return array(
                'factories' => array(
                        // DB
                        'UserTableGateway' => function ($sm) {
                            $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                            $resultSetPrototype = new ResultSet();
                            $resultSetPrototype->setArrayObjectPrototype(new User());
                            return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
                        },
                        'Admin\Model\UserTable' =>  function($sm) {
                            $tableGateway = $sm->get('UserTableGateway');
                            $table = new UserTable($tableGateway);
                            return $table;
                        },
                        'AdminProductTableGateway' => function ($sm) {
                            $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                            $resultSetPrototype = new ResultSet();
                            $resultSetPrototype->setArrayObjectPrototype(new AdminProduct());
                            return new TableGateway('sanpham', $dbAdapter, null, $resultSetPrototype);
                        },
                        'Admin\Model\AdminProductTable' =>  function($sm) {
                            $tableGateway = $sm->get('AdminProductTableGateway');
                            $table = new AdminProductTable($tableGateway);
                            return $table;
                        },
                        'BillTableGateway' => function ($sm) {
                            $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                            $resultSetPrototype = new ResultSet();
                            $resultSetPrototype->setArrayObjectPrototype(new Bill());
                            return new TableGateway('hoadon', $dbAdapter, null, $resultSetPrototype);
                        },
                        'Admin\Model\BillTable' =>  function($sm) {
                            $tableGateway = $sm->get('BillTableGateway');
                            $table = new BillTable($tableGateway);
                            return $table;
                        },
                        'AuthService' => function ($sm){
                            $dbAdapter = $sm -> get('Zend\Db\Adapter\Adapter');
                            $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter,'user','username','password','MD5(?)');
                            $authService = new AuthenticationService();
                            $authService->setAdapter($dbTableAuthAdapter);
                            $authService->setStorage($sm->get('Admin\Model\MyAuthStorage'));

                            return $authService;
                        },
                        'Admin\Model\MyAuthStorage' => function($sm){
                            return new \Admin\Model\MyAuthStorage;
                        }
                ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {

        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function loadConfiguration(MvcEvent $e){
        $application   = $e->getApplication();
        $sm            = $application->getServiceManager();
        $sharedManager = $application->getEventManager()->getSharedManager();
         
        $router = $sm->get('router');
        $request = $sm->get('request');
         
        $matchedRoute = $router->match($request);
        if (null !== $matchedRoute) { 
            $sharedManager->attach('Zend\Mvc\Controller\AbstractActionController','dispatch', function($e) use ($sm) {
                $sm->get('ControllerPluginManager')->get('Myplugin')->doAuthorization($e);
            },2);
        }
    }

    public function onDispatch(MvcEvent $e){
        // hàm dispatch.

    }
}
